﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HousingInfo.Library;

namespace NEWS.Admin
{
    public partial class NewsBussiness : System.Web.UI.Page
    {
        private static NewsBussinessBO newsbussinessbo;
        protected void Page_Load(object sender, EventArgs e)
        {
            string mode = Request.QueryString["Save"];
            //Mode Add
            if (mode == "Complete")
            {
                messagedelete.Text = "Đã Thêm";
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int bussinessid= Convert.ToInt32(GridView1.SelectedRow.Cells[3].Text);
            Response.Redirect("Edit_AddNewsBussiness.aspx?Mode="+ bussinessid);
        }

        protected void btndangtin_Click(object sender, EventArgs e)
        {
            Response.Redirect("Edit_AddNewsBussiness.aspx");
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string strScript = "SelectDeSelectHeader(" + ((CheckBox)e.Row.Cells[0].FindControl("chkSelect")).ClientID + ");";
                    ((CheckBox)e.Row.Cells[0].FindControl("chkSelect")).Attributes.Add("onclick", strScript);
                }
            }
            catch
            {
                //report error
            }     
        }

        protected void btnxoatin_Click(object sender, EventArgs e)
        {
            bool test = false;
            foreach (GridViewRow gvRow in GridView1.Rows)
            {
                CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect");
                if (chkSel.Checked == true)
                {
                    try
                    {
                        test = true;

                        int bussinessid = Convert.ToInt32(gvRow.Cells[3].Text);
                        newsbussinessbo= NewsBussinessBO.GetNewsBussinessBO(bussinessid);
                        NewsBussinessBO.DeleteNewsBussinessBO(bussinessid);
                    }
                    catch
                    {
                        messagedelete.Text = "Xóa thất bại";
                        return;
                    }
                }
            }
            if (test == false)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Chưa chọn thông tin cần xóa !!!')", true);
                return;
            }
            else
            {
                messagedelete.Text = "Xóa thành công !";
                GridView1.DataBind();
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "link")
            {
                string website = ((LinkButton)(((Control)e.CommandSource).Parent.Parent).FindControl("lbtwebsite")).Text;
                Response.Redirect(website);                
            }
        }
    }
}
